#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

extern char**  environ;

//获取父进程的环境变量，export来的自定义环境变量也可以被继承过来
int main(int argc, char* argv[], char* env[])
{
    //for(int i = 0; environ[i]; i++)
    //{
    //    printf("environ[%d]: %s\n", i, environ[i]);
    //}

    //std::cout << "Hello world" << std::endl;
    //for(int i = 0; i < argc; i++)
    //{
    //    printf("argv[%d]: %s\n", i, argv[i]);;
    //}
    char* str = getenv("PATH");//查询一个环境变量
    if(str == nullptr)
    {
        return -1;
    }
    printf("PATH:->%s\n", str);

    char* who = getenv("USER");
    if(who == nullptr)
    {
        return -1;
    }
    if(strcmp(who, "ubuntu") == 0)
    {
        cout << "you are allowed" << endl;
    }
    else 
    {
        cout << "Permission denied" << endl;
    }

    for(int i = 0; env[i]; i++)
    {
        printf("env[%d]: %s\n", i, env[i]);
    }

    if(argc != 2)
    {
        printf("usage:%s, [-a//-b//-c]\n", argv[0]);
        return 1;
    }

    char* arg = argv[1];
    if(strcmp(arg, "-a") == 0)
    {
        printf("function 1\n");
    }
    else if(strcmp(arg, "-b") == 0)
    {
        printf("function 2\n");
    }
    else if(strcmp(arg, "-c") == 0)
    {
        printf("function 3\n");
    }
    else
    {
        printf("wrong command\n");
    }

    return 0;
}

